/**
 * 
 */
package com.itinsys.enma.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.itinsys.enma.dao.DBTemplate;
import com.itinsys.enma.util.CareersIntTranspObject;

/**
 * @author pferraz
 *
 */
public class EnmaDataDAOImpl extends AbstractDataDAO {
	private static final String CAREER_NAME="CAREER_NAME";
	private static Logger logger = Logger.getLogger("dao");
	
	@Override
	public CareersIntTranspObject getCareers() {
		
		JdbcTemplate jTemplate = new JdbcTemplate(DBTemplate.getMysqlDataSource());
		List<Map<String,Object>> lMap;
		logger.info("Consultando las carreras con sus materias de la base de datos enma\n"+jTemplate);	
		lMap = jTemplate.queryForList("SELECT DISTINCT m.description,ca.description as CAREER_NAME,m.career_year FROM subjects m JOIN careers ca ON m.idCareer=ca.id ORDER BY m.description");
		
		return convertToCareerList(lMap,CAREER_NAME,"career_year","description");
	}

	
	
	
	@Override
	public String getSchoolIdByCareerName(String careerName) {
		JdbcTemplate jTemplate = new JdbcTemplate(DBTemplate.getMysqlDataSource());
		Map<String,Object> map;
		logger.info("Pidiendo el schoolId a traves del nombre de carrera\n"+jTemplate);
		String result="";
		try{
			map = jTemplate.queryForMap("SELECT DISTINCT(idSchool) as SCHOOL_ID FROM careers WHERE description='"+careerName+"'");
			result=map.get("SCHOOL_ID").toString();
		}catch(EmptyResultDataAccessException e){
			logger.debug("No se encontro la facultad por la carrera en enma");
		}
		return result;
	}

}
